﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.ComponentModel;

namespace yq.ControllerBase
{
    /// <summary>
    /// 多语言转换（需要继承）
    /// </summary>
    public abstract class LanguageUser
    {
        /// <summary>
        /// 保存Key
        /// </summary>
        private const string Key = "LanguageUser";

        /// <summary>
        /// 保存时间
        /// </summary>
        private const int AddDay = 30;

        /// <summary>
        /// 设置语言
        /// </summary>
        /// <param name="Enum"></param>
        public static void SetLanguage(ConfigEnum Enum)
        {
            HttpCookie cookie = new HttpCookie(Key, Enum.GetHashCode().ToString());
            cookie.Expires = DateTime.Now.AddDays(AddDay);
            HttpContext.Current.Response.Cookies.Add(cookie);
            HttpContext.Current.Request.Cookies.Add(cookie);
        }

        /// <summary>
        /// 获取当前语言
        /// </summary>
        /// <param name="Enum"></param>
        public static ConfigEnum GetLanguage()
        {
            var cookie = HttpContext.Current.Request.Cookies[Key];

            if (cookie == null || cookie.Value == null)
            {
                return ConfigEnum.Default;
            }
            else
            {
                return (ConfigEnum)Convert.ToInt32(cookie.Value);

            }
        }

        /// <summary>
        /// 转换语言
        /// </summary>
        /// <param name="Enum"></param>
        public string LanguageString(string txt)
        {
            return LanguageString(txt, GetLanguage());
        }

        /// <summary>
        /// 转换语言
        /// </summary>
        /// <param name="Enum"></param>
        public abstract string LanguageString(string txt,ConfigEnum config);
    }

    /// <summary>
    /// 语言配置
    /// </summary>
    public enum ConfigEnum
    {
        [Description("Default")]
        Default = -1,

        [Description("Zh_CN")]
        Zh_CN = 1,

        [Description("EN")]
        EN = 2,

        [Description("Zh_TW")]
        Zh_TW = 3
    }
}
